<?php declare(strict_types=1);

namespace App\Api\V2\Controller\Elec;

use App\Api\Controllers\BaseController;
use App\Models\Elec\ElecContracts;
use App\Services\ElectronicSignature\Bootstrap;
use App\Services\ElectronicSignature\ElectronicSignature;
use App\Traits\ResponseTrait;
use Illuminate\Http\Request;

class ContractController extends BaseController
{

    use ResponseTrait;

    public function createContract(Request $request): string
    {
        try {
            $code = $request->post("code");
            if (empty($code)) {
                throw new \Exception("请输入code");
            }
            $store_id = $request->post("store_id");

            $company = $request->post("company", "easypay");

            $elec = new ElectronicSignature(Bootstrap::run($request));
            $result = $elec->createContract([
                "code" => $code,
                "store_id" => $store_id,
                "company" => $company,
                "remark" => $request->post("remark", ""),
                "randomCode" => $request->post("randomCode", ""),
            ]);
            return $this->successResponse("ok", [
                "contractState" => $result["contractState"] ?? 0,
                "contractNo" => $result["contractNo"] ?? ""
            ]);
        } catch (\Exception $e) {
            return $this->errorResponse($e->getMessage());
        }
    }


    public function agreeContract(Request $request): string
    {
        try {
            $store_id = $request->post("store_id");
            if (empty($store_id)) {
                throw  new \Exception("店铺ID不能为空");
            }
            $company = $request->post("company", "easypay");
            $elec = new ElectronicSignature(Bootstrap::run($request));
            $result = $elec->agreeContract([
                "store_id" => $store_id,
                "company" => $company,
            ]);
            return $this->successResponse("ok", $result);
        } catch (\Exception $e) {
            return $this->errorResponse($e->getMessage());
        }
    }

    public function queryContract(Request $request): string
    {
        try {
            $elec = new ElectronicSignature(Bootstrap::run($request));
            $result = $elec->queryContract([
                "store_id" => $request->post("store_id"),
                "company" => $request->post("company", "easypay"),
            ]);
            return $this->successResponse("ok", $result ?? []);
        } catch (\Exception $e) {
            return $this->errorResponse($e->getMessage());
        }
    }

    //下载合同
    public function downloadContract(Request $request): string
    {
        try {
            $elec = new ElectronicSignature(Bootstrap::run($request));
            $result = $elec->downloadContract([
                "store_id" => $request->post("store_id"),
                "company" => "easypay",
            ]);
            return $this->successResponse("ok");
        } catch (\Exception $e) {
            return $this->errorResponse($e->getMessage());
        }
    }


    //合同列表
    public function listContract(Request $request): string
    {
        try {
            $where = [
                "page" => $request->get("page", 1),
                "pagesize" => (int)$request->get("pagesize", 20),
                "store_id" => $request->get("store_id"),
            ];
            $query = ElecContracts::query()->where("store_id", $where["store_id"]);
            $query->where("api_type", "easypay");
            $total = (clone $query)->count();
            $list = (clone $query)->orderBy("id", "desc")->limit($where["pagesize"])->offset(
                ($where["page"] - 1) * $where["pagesize"]
            )->get()->toArray();
            return $this->successResponse("ok", [
                "total" => $total,
                "list" => $list
            ]);
        } catch (\Exception $e) {
            return $this->errorResponse($e->getMessage());
        }
    }

    //作废成功的合同
    public function cancelContract(Request $request): string
    {
        try {
            $elec_contract_id = $request->post("elec_contract_id");
            if (empty($elec_contract_id)) {
                throw new \Exception("合同ID");
            }
            $elec = ElecContracts::query()->find($elec_contract_id);
            if (empty($elec)) {
                throw new \Exception("电子合同不存在");
            } else if ($elec->contractState == 1) {
                throw new \Exception("只能作废已完成的电子合同,用于重新签署合同");
            }
            $elec->contractState = -1;
            $elec->save();
            return $this->successResponse("ok");
        } catch (\Exception $e) {
            return $this->errorResponse($e->getMessage());
        }
    }

    public function detailContract(Request $request): string
    {
        try {
            $elec_contract_id = $request->get("elec_contract_id");
            if (empty($elec_contract_id)) {
                throw new \Exception("合同ID不存在");
            }
            //读取内容
            $elec = ElecContracts::query()->where("id", $elec_contract_id)->first();
            if (empty($elec)) {
                throw new \Exception("电子合同不存在");
            }
            return $this->successResponse("ok", $elec ?? []);
        } catch (\Exception $e) {
            return $this->errorResponse($e->getMessage());
        }
    }

}